/***************************************************************************
 *   KShootout - online shootout card game.                                *
 *   Copyright (C) 2008, 2009  MacJariel                                   *
 *                                                                         *
 *   KShootout is free software: you can redistribute it and/or modify     *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   KShootout is distributed in the hope that it will be useful,          *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with KShootout.  If not, see <http://www.gnu.org/licenses/>.    *
 ***************************************************************************/

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "config.h"
#include "util.h"
#include "game.h"
#include "serverconnection.h"
#include "application.h"

#include <QStackedLayout>
#include <QMessageBox>

using namespace client;


MainWindow::MainWindow():
        mp_ui(new Ui::MainWindow)
{
    mp_ui->setupUi(this);
    setStyleSheet(styleSheet() +
                  QString("#mp_centralWidget {\n"
                          "    background-image: url(\"%1\");\n"
                          "}\n").arg(Config::systemDataLocation() +
                                     "gfx/misc/kshootout-artwork.png"));
    delete mp_ui->centralWidget->layout();
    setWindowTitle(Application::applicationName());
    mp_stackedLayout = new QStackedLayout(mp_ui->centralWidget);

#if 0
    createActions();
    updateActions();
#endif

    connect(ServerConnection::instance(), SIGNAL(statusChanged()),
            this, SLOT(updateActions()));
}

/* virtual */
MainWindow::~MainWindow()
{
    delete mp_ui;
}

void MainWindow::showWarningMessage(const QString& message)
{
    QMessageBox* msgBox = new QMessageBox(0);
    msgBox->setWindowTitle(QApplication::applicationName());
    msgBox->setText(message);
    msgBox->setIcon(QMessageBox::Warning);
    connect(msgBox, SIGNAL(finished(int)), msgBox, SLOT(deleteLater()));
    QTimer::singleShot(0, msgBox, SLOT(exec()));
}

void MainWindow::setMainWidget(QWidget* widget)
{
    int index = mp_stackedLayout->indexOf(widget);
    if (index == -1) {
        index = mp_stackedLayout->addWidget(widget);
    }
    mp_stackedLayout->setCurrentIndex(index);
}

#if 0 /// to make it compile

/* slot */
void MainWindow::showConnectToServerDialog()
{
    ConnectToServerDialog* dlg = findChild<ConnectToServerDialog*>();
    if (!dlg) {
        dlg = new ConnectToServerDialog(this);
        connect(dlg, SIGNAL(connectToServer(QString, int)),
                ServerConnection::instance(), SLOT(connectToServer(QString, int)));
    }
    dlg->show();
}

/* slot */
void MainWindow::showCreateGameDialog()
{
    CreateGameDialog* dlg = findChild<CreateGameDialog*>();
    if (!dlg) {
        dlg = new CreateGameDialog(this);
        connect(dlg,SIGNAL(createGame(CreateGameData, CreatePlayerData)),
                ServerConnection::instance(), SLOT(createGame(CreateGameData, CreatePlayerData)));
    }
    dlg->show();
}

/* slot */
void MainWindow::showJoinGameDialog()
{
    JoinGameDialog* dlg = findChild<JoinGameDialog*>();
    if (!dlg) {
        dlg = new JoinGameDialog(this);
        connect(dlg, SIGNAL(joinGame(int, int, QString, CreatePlayerData)),
                ServerConnection::instance(), SLOT(joinGame(int, int, QString, CreatePlayerData)));
    }
    dlg->show();
}

/* slot */
void MainWindow::showAboutDialog()
{
    AboutDialog* dlg = findChild<AboutDialog*>();
    if (!dlg) {
        dlg = new AboutDialog(this);
    }
    dlg->show();
}

void MainWindow::showOptionsDialog()
{
    OptionsDialog* dlg = findChild<OptionsDialog*>();
    if (!dlg) {
        dlg = new OptionsDialog(this);
    }
    dlg->show();
}


void MainWindow::updateActions()
{
    if (ServerConnection::instance()->isConnected()) {
        mp_ui->actionConnectToServer->setEnabled(0);
        mp_ui->actionDisconnectFromServer->setEnabled(1);
        if (Application::instance()->mode() == Application::GameMode) {
            mp_ui->actionCreateGame->setEnabled(0);
            mp_ui->actionJoinGame->setEnabled(0);
            mp_ui->actionLeaveGame->setEnabled(1);
        } else {
            mp_ui->actionCreateGame->setEnabled(1);
            mp_ui->actionJoinGame->setEnabled(1);
            mp_ui->actionLeaveGame->setEnabled(0);
        }
    } else {
        mp_ui->actionConnectToServer->setEnabled(1);
        mp_ui->actionDisconnectFromServer->setEnabled(0);
        mp_ui->actionCreateGame->setEnabled(0);
        mp_ui->actionJoinGame->setEnabled(0);
        mp_ui->actionLeaveGame->setEnabled(0);
    }
}

void MainWindow::createActions()
{
    connect(mp_ui->actionConnectToServer, SIGNAL(triggered()),
            this, SLOT(showConnectToServerDialog()));
    connect(mp_ui->actionDisconnectFromServer, SIGNAL(triggered()),
            ServerConnection::instance(), SLOT(disconnectFromServer()));
    connect(mp_ui->actionCreateGame, SIGNAL(triggered()),
            this, SLOT(showCreateGameDialog()));
    connect(mp_ui->actionJoinGame, SIGNAL(triggered()),
            this, SLOT(showJoinGameDialog()));
    connect(mp_ui->actionLeaveGame, SIGNAL(triggered()),
            ServerConnection::instance(), SLOT(leaveGame()));
    connect(mp_ui->actionAbout, SIGNAL(triggered()),
            this, SLOT(showAboutDialog()));
    connect(mp_ui->actionCardSizeUp, SIGNAL(triggered()),
            &CardWidgetSizeManager::instance(), SLOT(cardSizeUp()));
    connect(mp_ui->actionCardSizeDown, SIGNAL(triggered()),
            &CardWidgetSizeManager::instance(), SLOT(cardSizeDown()));
    connect(mp_ui->actionOptions, SIGNAL(triggered()),
            this, SLOT(showOptionsDialog()));
}

void MainWindow::paintEvent(QPaintEvent* event)
{
    QPainter painter(this);
    QRadialGradient g(width() / 2, height() / 2, width() / 2, width() / 2 , height() / 2);
    g.setColorAt(0, QColor(239, 215, 179));
    g.setColorAt(0.5 , QColor(211, 179, 140));
    painter.fillRect(event->rect(), g);
    QMainWindow::paintEvent(event);
}

#endif